/**
 * 
 */
package com.winit.openapi.fsp.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.mms.spi.winitproduct.WinitProductCategoryService;
import com.winit.mms.spi.winitproduct.command.GetWinitProductCategoryCommand;
import com.winit.mms.spi.winitproduct.vo.ProductCategory;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * fsp 资信处理
 * 
 * @author
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/fsp/winitCategory")
public class FSPWinitCategoryInfoController extends BaseController {

    @Resource
    private WinitProductCategoryService winitProductCategoryService;

    /**
     * 查询winit产品分类
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/query")
    public String query(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetWinitProductCategoryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = buildQueryGoodsByGoodsCodeCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        List<ProductCategory> productCategoryList = winitProductCategoryService.getWinitProductCategory(command);
        if (productCategoryList != null) {
            List<Map<String, Object>> data = buildReturnProductCategoryList(productCategoryList);
            responseMsg.setData(data);
        }

        return SUCCESS;
    }
    
    /**
     * 根据最后的分类编码查询所有的父分类
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetWinitProductCategoryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = buildQueryGoodsByGoodsCodeCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        List<ProductCategory> productCategoryList = winitProductCategoryService.getWinitProductCategoryBycategoryCode(command);
        if (productCategoryList != null) {
            List<Map<String, Object>> data = buildReturnProductCategoryList(productCategoryList);
            responseMsg.setData(data);
        }

        return SUCCESS;
    }

    private List<Map<String, Object>> buildReturnProductCategoryList(List<ProductCategory> productCategoryList) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        Collections.reverse(productCategoryList);
        for (ProductCategory productCategory : productCategoryList) {
            map = new HashMap<String, Object>();
            map.put("categoryCode", productCategory.getCategoryCode());
            map.put("categoryName", productCategory.getCategoryName());
            map.put("categoryParentCode", productCategory.getCategoryParentCode());
            map.put("categoryId", productCategory.getCategoryId());
            mapList.add(map);
        }
        return mapList;
    }

    private GetWinitProductCategoryCommand buildQueryGoodsByGoodsCodeCommand(JSONObject json) {
        GetWinitProductCategoryCommand command = new GetWinitProductCategoryCommand();
        if (json.containsKey("categoryCode")) {
            command.setCategoryCode(json.getString("categoryCode"));
        }
        return command;
    }
}
